package com.mtrhz.xxx.service.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.mtrhz.esb.inteface.sso.MemberCallback;
import com.mtrhz.esb.inteface.sso.MemberSubscribeService;

public class MemberSubscribeServiceImpl implements MemberSubscribeService {
	
    private final Map<String, MemberCallback> listeners = new ConcurrentHashMap<String, MemberCallback>();

    public MemberSubscribeServiceImpl() {
        Thread t = new Thread(() -> {
            while (true) {
                try {
                    for (Map.Entry<String, MemberCallback> entry : listeners.entrySet()) {
                        try {
                            entry.getValue().pushChangedUsers(getChangedUser(entry.getKey()));
                        } catch (Throwable t1) {
                        	t1.printStackTrace();
                            listeners.remove(entry.getKey());
                        }
                    }
                    Thread.sleep(5000);
                } catch (Throwable t1) {
                    t1.printStackTrace();
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }
    
	@Override
	public void subscribe(String key, MemberCallback callback) {
		listeners.put(key, callback);
	}
    private String getChangedUser(String key) {
        return "json_list";
    }
}
